package com.ns.scs.my.controller;

import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.ads.service.IAdsApplyService;
import com.ns.scs.collections.service.ICollectionsService;
import com.ns.scs.comments.service.ICommentsService;
import com.ns.scs.msg.service.IMsgUserService;
import com.ns.scs.my.vo.MyMsgVO;
import com.ns.scs.rsr.service.IRsrApplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务WEB接口
 *
 * @author lw
 * @since 2024/09/11
 */
@RestController
@RequestMapping("/my")
@RequiredArgsConstructor
@CrossOrigin
public class MyInfoController {

    private final IAdsApplyService adsService;

    private final IRsrApplyService resourcesApplyService;

    private final ICollectionsService collectionsService;

    private final ICommentsService commentsService;

    private final IMsgUserService msgUserService;


    /**
     * 获取投放信息
     *
     * @param type 类型
     */
    @GetMapping("settled/{type}")
    @ReqLog(module = "投放模块", name = "我的投放")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getSettledInfo(@PathVariable String type) {
        return switch (type) {
            case "1" -> ResultOk.getSuccessData(resourcesApplyService.getApplyByUid(StpUtil.getLoginUserId()));
            case "2" -> ResultOk.getSuccessData(adsService.getAdsByUid(StpUtil.getLoginUserId()));
            default -> throw new ResultError("未知投放类型！");
        };
    }

    /**
     * 获取我的收藏
     *
     * @param type 类型
     */
    @GetMapping("collections/{type}")
    @ReqLog(module = "收藏模块", name = "我的收藏")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getCollections(@PathVariable String type) {
        return ResultOk.getSuccessData(collectionsService.getByType(type));
    }


    /**
     * 获取我的评论
     *
     * @param type 类型
     */
    @GetMapping("comments/{type}")
    @ReqLog(module = "评论模块", name = "我的评论")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getComments(@PathVariable String type) {
        return ResultOk.getSuccessData(commentsService.getByResourceIdAndUidAndType(null, StpUtil.getLoginUserId(), type));
    }

    /**
     * 我的消息
     *
     * @param status 状态
     */
    @GetMapping("msg")
    @ReqLog(module = "消息模块", name = "我的消息")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk myMsg(Integer status) {
        List<MyMsgVO> msgList = msgUserService.getMsgByUid(StpUtil.getLoginUserId(), StpUtil.getSid(), status);
        return ResultOk.getSuccessData(msgList);
    }

    /**
     * 我的消息
     *
     * @param bo id集合
     */
    @PostMapping("/msg-del")
    @ReqLog(module = "消息模块", name = "删除我的消息")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk delMyMsg(@RequestBody IdsBO bo) {
        try {
            return ResultOk.getSuccessData(msgUserService.removeByIds(bo.getIds()));
        } catch (Exception e) {
            throw new ResultError("删除失败");
        }
    }

    /**
     * 读消息
     *
     * @param bo id集合
     */
    @PostMapping("/msg-read")
    @ReqLog(module = "消息模块", name = "批量读消息")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk readMyMsg(@RequestBody IdsBO bo) {
        try {
            msgUserService.updateByIds(bo.getIds());
            return ResultOk.getSuccess();
        } catch (Exception e) {
            throw new ResultError("读消息失败");
        }
    }
}
